package net.halflite.mzkr.action.home;

import javax.annotation.Resource;

import net.halflite.mzkr.form.home.ProfileForm;
import net.halflite.mzkr.service.RegistedUserService;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import org.seasar.struts.enums.SaveType;

/** プロフィール修正のアクションクラス */
public class ProfileAction extends LoginedBaseAction {

	@Resource
	private RegistedUserService registedUserService;

	@ActionForm
	@Resource(name = "home_profileForm")
	private ProfileForm form;

	@Execute(validator = false)
    public String index() {
		registedUserService.setProfile(form, findUserId());
        return "input?redirect=true";
    }

	@Execute(validate = "validateUserId", input = "./?redirect=true")
    public String input() {
        return "input.jsp";
    }

	@Execute(validate = "validateChangeAddress", saveErrors = SaveType.SESSION, input = "input?redirect=true")
    public String validate() {
        return "confirm?redirect=true";
    }

	@Execute(validate = "validateUserId", input = "./?redirect=true")
    public String confirm() {
        return "confirm.jsp";
    }

	@Execute(validator = false)
    public String back() {
        return "input?redirect=true";
    }

	@Execute(validator = false)
    public String post() {
		registedUserService.postProfile(form, findUserId());
        return "success?redirect=true";
    }

	@Execute(validator = false, removeActionForm = true)
    public String success() {
        return "success.jsp";
    }

    /** 変更メールアドレス検証 */
    public ActionMessages validateChangeAddress() {
		return registedUserService.validateChangeAddress(form.address, findUserId());
    }

    /** ユーザーID検証 */
    public ActionMessages validateUserId() {
		ActionMessages result = new ActionMessages();
		if (form.registedUserId != findUserId()) result.add("registedUserId", new ActionMessage("errors.required"));
		return result;
    }
}
